﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using EntityManage;
using BusinessFacade;

public partial class Modules_Default_AppealApproveAdd : System.Web.UI.Page
{
    IPerf_Flow_TraceHandler traceHandler = new Perf_Flow_TraceManage();
    IPerf_FlowHandler flowHandler = new Perf_FlowManage();
    IPerf_Flow_StepsHandler stepsHandler = new Perf_Flow_StepsManage();
    ISys_UserHandler userHandler = new Sys_UserManage();
    IPerf_Flow_TraceDtlHandler traceDtlHandler = new Perf_Flow_TraceDtlManage();
    IPerf_Flow_TraceAttachHandler traceAttachHandler = new Perf_Flow_TraceAttachManage();

    IOther_AppealHandler appealHandler = new Other_AppealManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckPurviews())
        {
            Response.Redirect("~/Login.aspx");
        }
        if (!IsPostBack)
        {
            
        }
    }

    private bool CheckPurviews()
    {
        if (Session["UserPurviews"] != null)
        {
            string strPur = Session["UserPurviews"].ToString();
            if (strPur.Contains("303050"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string[] ttIds = Request["ttIds"].Split(',');
        int userId = int.Parse(Session["CurrentUserID"].ToString());
        Sys_UserQuery userQuery = userHandler.GetSys_UserQuery(userId);

        HttpFileCollection files = HttpContext.Current.Request.Files;

        // 调用公共方法，处理流程
        bool isLastStep = false;
        int isPast = int.Parse(rblIsPass.SelectedValue);
        FlowTrace.ProcessFlowTraceStep(ttIds, txtComment.Text, isPast, userQuery, files,ref isLastStep);
        
        foreach (string tmpId in ttIds)
        {
            int traceId = int.Parse(tmpId);
            Perf_Flow_TraceQuery traceQuery = traceHandler.GetPerf_Flow_TraceQuery(traceId);
            Other_AppealQuery appealQuery = appealHandler.GetOther_AppealQuery(traceQuery.Business_ID);

            // 如果审批不通过，或者到了流程的最后一步，更新评分表和呈批表的状态
            if (isPast != 0 || isLastStep == true)
            {
                appealQuery.Status = isPast == 0 ? (int)Enums.OtherAppealStatus.Passed : (int)Enums.OtherAppealStatus.NotPassed;
                appealHandler.UpdateOther_Appeal(appealQuery);

            }

            // 如果流程最后一步，而且审批通过
            if (isLastStep == true && isPast == 0)
            {
                //// 将评分表和呈批表状态改为公示
                appealQuery.Status = (int)Enums.OtherAppealStatus.Passed;
                appealHandler.UpdateOther_Appeal(appealQuery);

            }
        }

        Jscript.ReturnAndClose(this.Page, "true");
    }

}
